package com.freemyleft.left.left_app.mian.index.work.roborder;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;
import com.freemyleft.left.left_app.mian.index.work.roborder.adapter.RobOrderAdapter;
import com.freemyleft.left.left_app.mian.index.work.roborder.adapter.RobOrderConvert;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.ui.recycler.MultipleItemEntity;
import com.freemyleft.left.zapp.util.LeftPreference;
import com.google.gson.JsonObject;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * 抢单页面
 */
public class RobOrderDelegate extends LeftDelegate implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R2.id.recy)
    RecyclerView recyclerView;
    @BindView(R2.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private ArrayList<MultipleItemEntity> data;
    boolean isRobOrder=false;
    private String userid;
    private String tonken;
    private String id;

    @Override
    public Object setLayout() {
        return R.layout.delegate_work_rob_order;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        refreshLayout.setOnRefreshListener(this);
        inintData();
    }

    private void inintData() {
        userid = LeftPreference.getCustomAppProfile(TEACHER_ID);
        tonken = LeftPreference.getCustomAppProfile(TOKEN);
        final Map<String, Object> params = new HashMap<>();
        params.put("userid", userid);
        params.put("token", tonken);
        RestClient.builder().loader(getContext())
                .url("/home/teacher/search_list")
                .params(params)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        Log.e("responnse", responnse.toString());
                        refreshLayout.setRefreshing(false);
                        if (data != null) {
                            data.clear();
                        }
                        data = new RobOrderConvert().setJsonData(responnse).convert();
                        if (data != null) {
                            RobOrderAdapter adapter = new RobOrderAdapter(data);
                            LinearLayoutManager manager = new LinearLayoutManager(getContext());
                            manager.setOrientation(LinearLayoutManager.VERTICAL);
                            recyclerView.setAdapter(adapter);
                            recyclerView.setLayoutManager(manager);
                            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(final BaseQuickAdapter adapter, final View view, final int position) {
                                    MultipleItemEntity entity = (MultipleItemEntity) adapter.getData().get(position);
                                    id = entity.getField(RobOrderAdapter.RobOrderFiles.ID);
                                    if (view.getId() == R.id.btn_rob_order) {
                                        if (isRobOrder){
                                            RestClient.builder().loader(getContext())
                                                    .params("teacherid", userid)
                                                    .params("id",id)
                                                    .params("token",tonken)
                                                    .success(new ISuccess() {
                                                        @Override
                                                        public void onSuccess(String responnse) {
                                                            //////
                                                            JSONObject object = JSON.parseObject(responnse);
                                                            if (object.getString("code").equals("1000")) {
                                                                adapter.remove(position);
                                                                Toast.makeText(_mActivity, object.getString("msg"), Toast.LENGTH_SHORT).show();
                                                            } else if (object.getString("code").equals("1004")) {
                                                                Toast.makeText(_mActivity, object.getString("msg"), Toast.LENGTH_SHORT).show();
                                                            }
                                                        }
                                                    }).buid().post();
                                        }
                                        RestClient.builder()
                                                .loader(getContext())
                                                .url("/home/teacher/searchteacher_grab")
                                                .params("userid", userid)
                                                .params("token", tonken)
                                                .params("id", id)
                                                .success(new ISuccess() {
                                                    ImageView imageView = view.findViewById(R.id.btn_rob_order);
                                                    TextView textView = view.findViewById(R.id.tv_status);
                                                    @Override
                                                    public void onSuccess(String responnse) {
                                                        //设置抢单的状态变化
                                                        JSONObject object = JSONObject.parseObject(responnse);
                                                        if (object.getString("code").equals("1000")) {
                                                            textView.setText("抢单中");
                                                            Toast.makeText(_mActivity, object.getString("msg"), Toast.LENGTH_SHORT).show();
                                                        } else if (object.getString("code").equals("1001")) {
                                                            imageView.setImageResource(R.mipmap.quxiao);
                                                            isRobOrder = true;
                                                            Toast.makeText(_mActivity, object.getString("msg"), Toast.LENGTH_SHORT).show();
                                                        } else {
                                                            Toast.makeText(_mActivity, "登录失效", Toast.LENGTH_SHORT).show();
                                                        }
                                                    }
                                                }).buid().post();
                                    }
                                }
                            });
                        } else {
                            recyclerView.setVisibility(View.GONE);
                        }
                    }
                }).buid().post();

    }


    @Override
    public void onRefresh() {
        inintData();

    }
}

